/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.GUI.sidepanels;

import controller.commands.BasicCommand;
import controller.commands.CloneCommand;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import model.graphic.objects.GBoxNode;
import view.GUI.LabelReader;
import view.GUI.popup.PalettePopup;

/**
 *
 * @author Jelle
 */
public class PaletteItem extends JButton implements DragGestureListener, DragSourceListener, IItem, MouseListener {

    private transient TransferableArrayList commands;
    private ImageIcon icon;
    private DragSource dragSource;
    private String packageName;
    private JTextField edit;
    private PalettePopup popup;
    private String info = "";
    private JTextArea ta;

    public PaletteItem(TransferableArrayList commands, String title, String container, String info) {
        super(title);
        this.commands = commands;
        this.packageName = container;
        this.info = info;
        this.icon = new ImageIcon("icons\\node.png");
        this.setIcon(icon);
        this.dragSource = new DragSource();
        this.dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE, this);
        this.setBackground(Color.WHITE);
        this.setToolTipText(LabelReader.getInstance().getHTMLString("info" + title));
        this.setFocusPainted(false);
        this.setIconTextGap(7);
        this.setHorizontalAlignment(LEFT);
        this.edit = new JTextField();
        this.edit.setVisible(false);
        this.addMouseListener(this);
        this.popup = new PalettePopup();
    }

    /**
     * @return the commands
     * 
     */
    public ArrayList<BasicCommand> getCommands() {
        return commands;
    }

    /**
     * @param commands the commands to set
     */
    public void setCommands(TransferableArrayList commands) {
        this.commands = commands;
    }

    public void dragGestureRecognized(DragGestureEvent evt) {
        Transferable t = (Transferable) commands;
        dragSource.startDrag(evt, DragSource.DefaultCopyDrop, t, this);
    }

    public void dragEnter(DragSourceDragEvent evt) {
        //System.out.println("enters");
    }

    public void dragOver(DragSourceDragEvent evt) {
        //System.out.println("over");
    }

    public void dragExit(DragSourceEvent evt) {
        //System.out.println("leaves");
    }

    public void dropActionChanged(DragSourceDragEvent evt) {
        //System.out.println("changes the drag action between copy or move");
    }

    public void dragDropEnd(DragSourceDropEvent evt) {
        //System.out.println("finishes or cancels the drag operation");
    }

    /**
     * @return the packageName
     */
    public String getPackageName() {
        return packageName;
    }

    /**
     * @return the edit
     */
    public JTextField getEdit() {
        return edit;
    }


    public void mouseClicked(MouseEvent e) {
        if (e.getClickCount() == 2) {
            final JLayeredPane jp = getRootPane().getLayeredPane();
            final JPanel p = new JPanel();
            p.setLayout(new BorderLayout());
            p.setLocation((int) this.getLocationOnScreen().getX() + getX() + 12, (int) this.getLocationOnScreen().getY() - 28);
            p.setSize(new Dimension(getWidth() + 2, getHeight() + 2));
            final EditableTextField et = new EditableTextField("NewName");

            et.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    setText(et.getText());
                    for (Object o : commands) {
                        if (CloneCommand.class.isAssignableFrom(o.getClass())) {
                            CloneCommand comm = (CloneCommand) o;
                            Palette palette = (Palette) getParent();
                            GBoxNode bn = palette.getGBoxNode(comm.getGBoxID());
                            bn.setName(et.getText());

                        }
                    }
                    et.setVisible(false);
                    jp.moveToFront(p);
                    jp.remove(p);
                }
            });
            p.add(et, BorderLayout.CENTER);
            jp.setLayer(p, 10);
            jp.add(p);
            et.requestFocus();
        }
        if (e.getClickCount() == 1) {
            PaletteItem pi = (PaletteItem) e.getSource();
            ta.setText(pi.getInfoAbout());
        }

    }

    public void mousePressed(MouseEvent e) {
        PaletteItem pi = (PaletteItem) e.getSource();
            ta.setText(pi.getInfoAbout());
    }

    public void mouseReleased(MouseEvent e) {
        popup.setPalette((Palette) this.getParent());
        popup.setPi(this);
        if (e.isPopupTrigger()) {
            popup.menuPanel();
            popup.getPopup().show(e.getComponent(), e.getX(), e.getY());
        }
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void addInfoPanelRef(JTextArea ta) {
        this.ta = ta;
    }

    private String getInfoAbout() {
        return info;
    }
}
